package com.csiec.clock.model;



import com.alibaba.fastjson.JSONObject;
import com.csiec.clock.bean.login.ResultCode;
import com.csiec.clock.callback.LoginCallBack;
import com.csiec.clock.utils.Constant;


import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class LoginModel {
    private final String www = Constant.www;
    private final String androidUrl = "/student/android";

    //登录的网络请求
    public void login(final String userName, final String passWord, final LoginCallBack callBack) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("userName", userName)
                        .add("passWord", passWord)
                        .build();
                Request request = new Request.Builder()
                        .url(www + androidUrl)
                        .post(body)
                        .build();//创建一个Request对象

                Response response = client.newCall(request).execute();//发送请求获取返回数据
                String responseData = response.body().string();//处理返回的数据
                ResultCode backData = JSONObject.parseObject(responseData, ResultCode.class);
                callBack.onSuccess(backData);

            } catch (Exception e) {
                callBack.onFailed();
                e.printStackTrace();
            }
        }).start();

    }


}
